
var req;
if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
} else {
	req = new ActiveXObject("Microsoft.XMLHTTP");
}
var xmlQueue = new Array();
var reqBusy = false;
var debug = false;
setInterval(checkQueue, 100);
function QueueItem(type, text) {
	this.type = type;
	this.text = text;
}
function checkQueue() {
	if (!reqBusy && xmlQueue.length > 0) {
		reqBusy = true;
		var item = xmlQueue.shift();
		var href = location.href;
		switch (item.type) {
		  case "POST":
			if (href.indexOf("?") > 0) {
				href = href + "&action=COMMAND";
			} else {
				href = href + "?action=COMMAND";
			}
			req.open("POST", href);
			req.onreadystatechange = responseHandler;
			req.send(item.text);
			break;
		  case "GET":
			if (href.indexOf("?") > 0) {
				href = href + "&action=" + item.text;
			} else {
				href = href + "?action=" + item.text;
			}
			req.open("GET", href);
			req.onreadystatechange = responseHandler;
			req.send();
			break;
		}
	}
}
function getInitialState() {
	var xml = "<state type='initialize'/>";
	postXML(xml);
}
function postXML(xml) {
	if (!xmlQueue) {
		xmlQueue = new Array();
	}
	var item = new QueueItem("POST", xml);
	xmlQueue.push(item);
}
function doGet(action) {
	if (!xmlQueue) {
		xmlQueue = new Array();
	}
	var item = new QueueItem("GET", action);
	xmlQueue.push(item);
}
function postUpdate(elementId) {
	if (!elementId) {
		elementId = this.id;
	}
	var element = getElement(elementId);
	var xml = "<state type='update'>\r\n";
	xml += serialize(element);
	xml += "</state>";
	postXML(xml);
}
function postAction(elementId, action, source) {
	var xml = "<state type='action'>\r\n";
	xml += "<elementid>" + elementId + "</elementid>\r\n";
	xml += "<action>" + action + "</action>\r\n";
	if (source) {
		xml += "<source>" + source + "</source>\r\n";
	}
	xml += "</state>";
	postXML(xml);
}
function responseHandler() {
	if (req.readyState == 4) {
		if (debug) {
			alert(req.responseText);
		}
		eval(req.responseText);
		reqBusy = false;
	}
}
function actionListener(e) {
	e = e || ((this.ownerDocument || this.document || this).parentWindow || window).event;
	var element = allElements[this.id];
	var keycode;
	if (e.keyCode) {
		keycode = e.keyCode;
	} else {
		keycode = e.which;
	}
	switch (e.type) {
	  case "change":
		if (element instanceof Input) {
			element.setValue(this.value);
		}
		if (element instanceof SelectBox) {
			element.select(this.selectedIndex);
		}
		postUpdate(element.id);
		postAction(element.id, "change");
		break;
	  case "keypress":
		if (element instanceof Input && keycode == 13) {
			element.setValue(this.value);
			postUpdate(element.id);
			postAction(element.id, "enter");
			return false;
		}
		return true;
	  default:
		if (element.actions && element.actions[e.type]) {
			postAction(element.id, element.actions[e.type]);
		} else {
			if (element.action) {
				postAction(element.id, element.action);
			} else {
				postAction(element.id, e.type);
			}
		}
	}
	return true;
}
function startFileUpload() {
	getElement(statusBarId).start();
}

